﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CanSWAT.ArcSWAT.Soil
{
    partial class SoilFrm : Form
    {
        public SoilFrm()
        {
            InitializeComponent();
        }

        private SoilDatabase _soilDatabase = null;
        public SoilDatabase SoilDatabase
        {
            set
            {
                _soilDatabase = value;
            }
        }

        private void SoilFrm_Load(object sender, EventArgs e)
        {
            showSoilStruture();
            rdbArcSWAT2009.Checked = true;
        }

        private void showSoilStruture()
        {
            if (_soilDatabase == null) return;

            treeView1.Nodes.Clear();
            foreach (SoilPolygon p in _soilDatabase.SoilPolygons)
            {
                TreeNode polygonNode = treeView1.Nodes.Add(p.ToString());
                List<SoilComponent> cmps = p.Components;

                foreach (SoilComponent cmp in cmps)
                {
                    TreeNode cmpNode = polygonNode.Nodes.Add(cmp.ToString());
                    foreach (SoilDeriv deriv in cmp.Derivs)
                        cmpNode.Nodes.Add(deriv.Depth.ToString());
                }
            }

            if (treeView1.Nodes.Count == 0)
                CanSWAT.showInformationWindow("No soil data is loaded. Please check input database!");
        }

        private void bBrowseSoilGDB_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _soilDatabase = new SoilDatabase(folderBrowserDialog1.SelectedPath);
                txtGDB.Text = folderBrowserDialog1.SelectedPath;
                showSoilStruture();
            }
        }

        private void bGenerate_Click(object sender, EventArgs e)
        {
            if (_soilDatabase == null)
            {
                MessageBox.Show("Please select soil GeoDatabase first!");
                return;
            }

            this.richTextBox1.Text = _soilDatabase.ToUserSoilTableString(
                this.rdbArcSWAT2009.Checked ? ArcSWATVersion.A2009 : ArcSWATVersion.A2012);
        }
    }
}
